home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / jam / jamdisk4 / arptools / src / entab.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  6KB  |  254 lines

  1. /*
  2.           EnTab - Replace blanks with TABs whenever possible.
  3.  
  4.           Based on the entab program by Gary Brant found on <>< 179.
  5.           ARP and Resident support by Fabio Rossetti.
  6.  
  7.           (c) 1989 by Fabio Rossetti
  8.  
  9.           To compile under Lattice C v5.0x use:
  10.  
  11.         lc -O -v -cus entab
  12.         blink lib:cres.o entab.o to entab lib lib:a.lib lib:lc.lib sd nd
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/dosextens.h>
  21. #include <libraries/arpbase.h>
  22. #include <arpfunctions.h>
  23.  
  24. #include <proto/exec.h>
  25. #include <proto/dos.h>
  26.  
  27. struct ArpBase *ArpBase=NULL;
  28. struct Process *Pr;
  29. LONG argc;
  30.  
  31. #define NARGS 3
  32. #define BFSIZE 256    
  33. #define FROM    argv[0]
  34. #define TO    argv[1]
  35. #define TAB    argv[2]
  36.  
  37. STRPTR argv[NARGS];
  38.  
  39. struct UserAnchor {
  40.     struct    AnchorPath    ua_AP;
  41.     BYTE    moremem[255];    /* extension */
  42. };
  43. struct    UserAnchor *Anchor=NULL;
  44.  
  45. /* Trick to keep code down to size */
  46. VOID MemCleanup()
  47. {
  48. }
  49.  
  50. /* shutdown routine */
  51. VOID Cleanup(r1,r2,msg)
  52. LONG r1,r2;
  53. STRPTR msg;
  54. {
  55.     if (msg) Puts(msg);
  56.     if (ArpBase) CloseLibrary((struct Library *)ArpBase);
  57.     Pr->pr_Result2 = r2;
  58.     exit(r1);
  59. }
  60.  
  61. #define MAXLINE 256
  62.  
  63. char tabarray[MAXLINE];
  64.  
  65.  
  66. VOID _main(Line)
  67. STRPTR Line;
  68.  
  69. {
  70.  
  71.    int i = 0, j, k, l, ntabs = 0, tabstop = 8;
  72.    int argtabs[99];
  73.    char ch;
  74.    char **Argv;
  75.     BPTR fh,ofh;
  76.  
  77.  
  78.  
  79.     Pr = (struct Process*)FindTask(NULL);
  80.     
  81.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion)))
  82.             Cleanup(RETURN_FAIL,ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  83.     
  84.     /* parse command line */
  85.     for (argc=0;argc < NARGS ;++argc)
  86.         argv[argc] = (STRPTR) NULL;
  87.  
  88.     while(*Line > ' ')
  89.         ++Line;
  90.  
  91.     if((argc = GADS(++Line,
  92.         strlen(Line),
  93.         "Usage: EnTab [FROM Pattern] [TO Filename] [TAB tabsize [tabsize ..] ]",
  94.         argv,
  95.         "FROM,TO,TAB/..." )) < 0)
  96.             Cleanup(RETURN_WARN,NULL,FROM);
  97.  
  98.  
  99.    /* decode tabstop arguments */
  100.    Argv = (char **)TAB;
  101.    while ((Argv[i] != NULL)) {
  102.       j = 0;
  103.       tabstop = 0;
  104.       while ((ch = (char)Argv[i++][j++]) != '\0') {
  105.      if (ch >= '0' && ch <= '9') {
  106.         tabstop *= 10;
  107.         tabstop += ch - '0';
  108.      } else
  109.         Cleanup(RETURN_WARN,NULL,"Bad args");
  110.       }
  111.       argtabs[ntabs++] = tabstop;
  112.    }
  113.  
  114.    /* fill tabarray with \1 for each tabstop position */
  115.    for (k = 0; k < MAXLINE; k++)
  116.       tabarray[k] = '\0';
  117.    if (ntabs > 1)
  118.       for (k = 0; k < ntabs; k++)
  119.      if ((l = argtabs[k]-1) < MAXLINE)
  120.         tabarray[l] = '\001';
  121.      else
  122.         Cleanup(RETURN_WARN,NULL,"Bad tab specification");
  123.    else if ((tabstop > 0) && (tabstop < MAXLINE))
  124.       for (k = tabstop; k < MAXLINE; k += tabstop)
  125.      tabarray[k] = '\001';
  126.    else Cleanup(RETURN_WARN,NULL,"Bad tab specification");
  127.  
  128.  
  129.     if ( Anchor = (struct UserAnchor *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  130.     {
  131.         Anchor->ua_AP.ap_Length = 255;    /* Want full path built */
  132.         
  133.     }
  134.     else Cleanup(RETURN_FAIL,ERROR_NO_FREE_STORE,"Error:No memory");
  135.     
  136.     if (FROM)  {
  137.  
  138.     if (!(FindFirst(FROM,(struct AnchorPath*) Anchor)))
  139.         if (Anchor->ua_AP.ap_Info.fib_DirEntryType < 0) {
  140.             if(!(fh=ArpOpen(Anchor->ua_AP.ap_Buf,MODE_OLDFILE))) {
  141.                 Printf("Can't open %s\n",FROM);
  142.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  143.                 }
  144.             }
  145.         else {
  146.             Printf("%s is a directory !\n",TO);
  147.             Cleanup(RETURN_ERROR,ERROR_OBJECT_WRONG_TYPE,NULL);
  148.             }
  149.         else {
  150.             Printf("Can't find %s\n",FROM);
  151.             Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  152.             }
  153.         }
  154.     else fh = Input();
  155.  
  156.     if(TO) {
  157.             if(!(ofh=ArpOpen(TO,MODE_NEWFILE))) {
  158.                 Printf("Can't open %s\n",TO);
  159.                 Cleanup(RETURN_ERROR,ERROR_OBJECT_NOT_FOUND,NULL);
  160.                 }
  161.             }
  162.     else ofh = Output();
  163.  
  164.         entab (fh,ofh);
  165.  
  166.     Cleanup(NULL,NULL,NULL);
  167.  
  168. }
  169.  
  170. /* entab - remove the tabs from one file */
  171.  
  172. entab (ifile,ofile)
  173. BPTR ifile,ofile;
  174. {
  175.    int n;
  176.    char inline[MAXLINE],Buf[MAXLINE], outline[MAXLINE];
  177.  
  178. register int count, obp=0;
  179. register char *il = inline;
  180.  
  181. /* read lines */
  182. for (;;) {
  183. if (!obp) if (!(count = Read(ifile,Buf,BFSIZE))) {        
  184.             return(0);
  185.             }
  186.  
  187. if (*(Buf + obp) == '\n') {
  188.         ++obp;
  189.         *il = '\0';
  190.         il = inline;
  191.  
  192.         if (SetSignal(0,0) & SIGBREAKF_CTRL_C) 
  193.         Cleanup(RETURN_WARN,NULL,"***Break");
  194.  
  195.  
  196.       n = strlen (inline);
  197.       while (--n >= 0)            /* back over white space */
  198.      if (inline[n] != ' ' && inline[n] != '\t' &&
  199.         inline[n] != '\n') break;
  200.       inline[n+1] = '\0';
  201.       compress (inline, outline, MAXLINE);
  202.       FPrintf(ofile,"%s\n",outline);
  203.    }
  204. else {
  205.     if (((char *)il - (char *)inline) > MAXLINE) 
  206.             Cleanup(RETURN_FAIL,NULL,"Buffer overflow");
  207.     *il++ = *(Buf + obp++);
  208.     }
  209. if (obp == count) obp = 0;
  210. }
  211. }
  212.  
  213.  
  214.  
  215. /* compress - compress one line, replacing strings of blanks with tabs    *
  216.  * to tab stops specified on command line or default            */
  217.  
  218. compress (in, out, lim)
  219. char in[], out[];
  220. int  lim;
  221. {
  222.    register int i = 0, j = 0;
  223.    register char ch;
  224.  
  225.    while (((ch = in[i++]) != '\0') && (i < lim)) {
  226.       if (ch == ' ') {
  227.      register int k = i, tc;
  228.      while (((tc = tabarray[k]) == '\0') && (in[k] == ' ') && (k < lim))
  229.         k++;
  230.      if ((tc == '\001') && (k > i))
  231.         out[j++] = '\t';
  232.      else            /* avoid running through this again next trip */
  233.         while (i++ <= k)
  234.            out[j++] = ch;
  235.      i = k;
  236.       } else if (ch == '\042' || ch == '\047') {
  237.      register int tc;        /* avoid tabbing quoted strings */
  238.      out[j++] = ch;
  239.      while (((tc = in[i++]) != ch) && (i < lim)) {
  240.         if (tc == '\134') {     /* possible protected quote */
  241.            out[j++] = tc;
  242.            tc = in[i++];
  243.            if (i == lim) break;
  244.         }
  245.         out[j++] = tc;
  246.      }
  247.      out[j++] = ch;
  248.       } else
  249.      out[j++] = ch;
  250.    }
  251.    out[j] = ch;
  252. }
  253.  
  254.